import { defineStore } from 'pinia';

interface UserInfo {
  userId: string;
  userName: string;
  token: string;
}

const userInfo = JSON.parse(localStorage.getItem('userInfo')!) || null;
export const useUserStore = defineStore('user', {
  state: () => {
    return {
      userInfo: userInfo as UserInfo | null
    };
  },
  actions: {
    login() {
      const mathStr = String(Math.random() * 1000);
      this.userInfo = {
        userName: '爱装逼的程序员',
        userId: mathStr,
        token: mathStr
      };
      localStorage.setItem('userInfo', JSON.stringify(this.userInfo));
    },
    logout() {
      localStorage.removeItem('userInfo');
      this.userInfo = null;
    }
  }
});
